package com.nanjia.boot.product.sku.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nanjia.boot.common.result.PageResult;
import com.nanjia.boot.common.result.Result;
import com.nanjia.boot.common.util.IPUtils;
import com.nanjia.boot.product.sku.model.form.ProductSkuConnectSpecValueForm;
import com.nanjia.boot.product.sku.model.form.ProductSkuForm;
import com.nanjia.boot.product.sku.model.query.ProductSkuQuery;
import com.nanjia.boot.product.sku.model.vo.ProductSkuVO;
import com.nanjia.boot.product.sku.service.ProductSkuService;
import com.nanjia.boot.product.sku.spec.value.model.entity.SkuSpecValue;
import com.nanjia.boot.product.sku.spec.value.model.vo.SpuSkuSpecGroupSpecValueVo;
import com.nanjia.boot.product.sku.spec.value.service.SkuSpecValueService;
import com.nanjia.boot.product.spu.model.entity.ProductSpu;
import com.nanjia.boot.product.spu.service.ProductSpuService;
import com.nanjia.boot.warehouse.model.form.WarehouseForm;
import com.nanjia.boot.warehouse.model.query.WarehouseQuery;
import com.nanjia.boot.warehouse.service.WarehouseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品库存单元(SKU)前端控制层
 *
 * @author 周洋
 * @since 2025-07-10 14:15
 */
@Tag(name = "商品库存单元(SKU)接口")
@RestController
@RequestMapping("/api/v1/product-sku")
@RequiredArgsConstructor
public class ProductSkuController {

    private final ProductSkuService productSkuService;

    private final ProductSpuService productSpuService;

    private final SkuSpecValueService skuSpecValueService;

    private final WarehouseService warehouseService;

    @Operation(summary = "商品库存单元(SKU)分页列表")
    @GetMapping("/page")
    public PageResult<ProductSkuVO> getProductSkuPage(ProductSkuQuery queryParams) {
        IPage<ProductSkuVO> result = productSkuService.getProductSkuPage(queryParams);
        return PageResult.success(result);
    }


    @Operation(summary = "获取商品库存单元(SKU)表单数据")
    @GetMapping("/{id}/form")
    public Result<ProductSkuForm> getProductSkuForm(
            @Parameter(description = "商品库存单元(SKU)ID") @PathVariable Long id
    ) {
        ProductSkuForm formData = productSkuService.getProductSkuFormData(id);
        return Result.success(formData);
    }

    @Operation(summary = "商品库存单元(SKU)分页列表")
    @GetMapping("/listSpu")
    public Result<List<ProductSpu>> listSpu(ProductSkuQuery queryParams) {
        List<ProductSpu> result = productSpuService.listSpu(queryParams);
        return Result.success(result);
    }

    @Operation(summary = "已选规格值关联表")
    @GetMapping("/listSkuSpecValue/{id}")
    public Result<List<SkuSpecValue>> listSkuSpecValueForm(@PathVariable Long id) {
        LambdaQueryWrapper<SkuSpecValue> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SkuSpecValue::getSkuId, id).orderByDesc(SkuSpecValue::getId);
        List<SkuSpecValue> result = skuSpecValueService.list(queryWrapper);

        return Result.success(result);
    }

    @Operation(summary = "所有规格值")
    @GetMapping("/getSpuSkuSpecGroupSpecValueVo/{id}")
    public Result<SpuSkuSpecGroupSpecValueVo> getSpuSkuSpecGroupSpecValueVo(@PathVariable Long id) {
        SpuSkuSpecGroupSpecValueVo vo =  skuSpecValueService.getSpuSkuSpecGroupSpecValueVo(id);
        return Result.success(vo);
    }

    @Operation(summary = "关联规格值")
    @PutMapping("/connectSpecValue/{id}")
    public Result<Boolean> connectSpecValue(
            @PathVariable Long id,
            @RequestBody ProductSkuConnectSpecValueForm form,HttpServletRequest request
    ) {
        boolean result = skuSpecValueService.connectSpecValue(id, form,IPUtils.getIpAddr(request));
        return Result.judge(result);
    }

//    @Operation(summary = "商品库存单元(SKU)分页列表")
//    @GetMapping("/listWarehouseForm")
//    public Result<List<WarehouseForm>> listWarehouseForm(WarehouseQuery queryParams) {
//        List<WarehouseForm> result = warehouseService.listWarehouseForm(queryParams);
//        return Result.success(result);
//    }

    @Operation(summary = "所有规格值")
    @GetMapping("/getListSpuSkuSpecGroupSpecValueVo/{id}")
    public Result<Map<String,Object>> getListSpuSkuSpecGroupSpecValueVo(@PathVariable Long id) {
        Map<String,Object> map;
        map =  skuSpecValueService.getListSpuSkuSpecGroupSpecValueVoBySpuId(id);

        return Result.success(map);
    }
}
